package com.system.controller;

import com.common.annotation.MyLog;
import com.system.entity.Dept;
import com.system.service.DeptService;
import com.system.vo.request.DeptAddReqVO;
import com.system.vo.request.DeptUpdateReqVO;
import com.system.vo.response.DeptRespNodeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.NotBlank;
import javax.validation.Valid;
import java.util.List;
import com.common.res.DataResult;


import javax.validation.Valid;
import java.util.List;

/**
* @author XueHeng
* @since 2020-09-17
*/
@RestController
@RequestMapping("api")
@Api(tags = "部门管理")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping("/depts")
    @ApiOperation("部门列表")
    @MyLog(title = "部门管理",action = "获取部门列表")
    @RequiresPermissions("sys:dept:list")
    public DataResult<List<Dept>> getAllDept(){
        DataResult result =DataResult.success();
        result.setData(this.deptService.getDepts());
        return result;
    }

    @GetMapping("/dept/tree")
    @ApiOperation("部门树")
    @MyLog(title = "部门管理",action = "获取部门树")
    @RequiresPermissions(value = {"sys:user:update","sys:user:add","sys:dept:add","sys:dept:update"},logical = Logical.OR)
    public DataResult<List<DeptRespNodeVO>> getDeptTree(@RequestParam(required = false) String deptId){
        DataResult result =DataResult.success();
        result.setData(this.deptService.deptTreeList(deptId));
        return result;
    }



    @PostMapping("/dept")
    @ApiOperation("新增")
    @MyLog(title = "部门管理",action = "新增部门")
    @RequiresPermissions("sys:dept:add")
    public DataResult add(@Valid @RequestBody DeptAddReqVO vo) {
        DataResult result =DataResult.success();
        this.deptService.add(vo);
        return result;
    }

    @PutMapping("/dept")
    @ApiOperation("更新")
    @MyLog(title = "部门管理",action = "更新部门")
    @RequiresPermissions("sys:dept:update")
    public DataResult update(@Valid @RequestBody DeptUpdateReqVO vo) {
        DataResult result =DataResult.success();
        this.deptService.update(vo);
        return result;
    }


    @DeleteMapping(value = "/dept/{deptId}")
    @ApiOperation("删除")
    @MyLog(title = "部门管理",action = "删除部门")
    @RequiresPermissions("sys:dept:delete")
    public DataResult remove(@NotBlank(message = "{required}") @PathVariable String deptId) {
        DataResult result =DataResult.success();
        this.deptService.delete(deptId);
        return result;
    }
}
